package com.zhuannimei.fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;

import com.zhuannimei.GlobalApplication;
import com.zhuannimei.R;
import com.zhuannimei.adapter.ProductAdapter;
import com.zhuannimei.domain.Product;
import com.zhuannimei.net.RequestServerData;
import com.zhuannimei.parser.ProductParser;
import com.zhuannimei.ui.JoinUsActivity;
import com.zhuannimei.utility.JsonUtil;
import com.zhuannimei.utility.WuLog;
import com.zhuannimei.view.ZnmRefreshListView;
import com.zhuannimei.view.ZnmRefreshListView.OnCancelListener;
import com.zhuannimei.view.ZnmRefreshListView.OnRefreshListener;

public class Fragment2 extends BaseFragment implements OnItemClickListener,
		OnRefreshListener, OnCancelListener {

	private static final String TAG = "Fragment2";
	private ZnmRefreshListView znmRL;// 赚你妹下拉控件

	private List<Product> mProducts = new ArrayList<Product>();

	private ProductAdapter mAdapter;

	private String mTimeStamp = "1";

	private int mPage = 0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// 准备数据
		// 先查本地数据
		// 然后查询互联网数据
		try {
			mProducts.clear();
			List<Product> list = GlobalApplication.appDB.findAll(Product.class);
			if (list != null && list.size() != 0) {
				mProducts.addAll(list);
			}
			getProductsFromNet();
		} catch (Exception e) {
		}

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.fragment2, container, false);
		return super.onCreateView(inflater, container, savedInstanceState);
	}

	@Override
	public void onStart() {
		super.onStart();
		mTitlePane.setTitle("积分商城");
	}

	@Override
	protected void initViews() {
		znmRL = (ZnmRefreshListView) findViewById(R.id.point_shop_ZR);
	}

	@Override
	protected void initEvents() {
		znmRL.setOnItemClickListener(this);
		znmRL.setOnRefreshListener(this);
		znmRL.setOnCancelListener(this);
	}

	@Override
	protected void initData() {
		if (znmRL != null) {
			mAdapter = new ProductAdapter(getActivity(), mProducts);
			znmRL.setAdapter(mAdapter);
			WuLog.i(TAG, "刷新界面");
			znmRL.onRefreshComplete();
		}
	}

	/**
	 * 从服务器上获取商品列表
	 */
	private void getProductsFromNet() {
//		putAsyncTask(new AsyncTask<Void, Void, String>() {
//
//			@Override
//			protected void onPreExecute() {
//				super.onPreExecute();
//				// showCustomToast("下载数据");
//			}
//
//			@Override
//			protected String doInBackground(Void... params) {
//				Map<String, String> map = new HashMap<String, String>();
//				map.put("pageindex", mPage + "");
//				map.put("timestamp", mTimeStamp);
//				Map<String, String> map2 = JsonUtil.getJsonMap(map);
//				return RequestServerData.productList(map2);
//			}
//
//			@Override
//			protected void onPostExecute(String result) {
//				super.onPostExecute(result);
//				WuLog.i(TAG, "执行结束，服务器返回的结果=====" + result);
//				try {
//					if (!TextUtils.isEmpty(result)) {
//						JSONObject jo = new JSONObject(result);
//						String vCode = jo.getString("result_code");
//						String desc = jo.getString("desc");
//						mTimeStamp = jo.optString("timestamp");
//						WuLog.i(TAG, "服务器返回的时间戳：" + mTimeStamp);
//						if ("0".equals(vCode)) {
//							// 解析json
//							mProducts.clear();
//							List<Product> list = new ProductParser()
//									.parseJSON(result);
//							if (list != null && list.size() != 0) {
//								mProducts.addAll(list);
//							}
//							WuLog.i(TAG, "集合的大小" + mProducts.size());
//							initData();
//							// 先删后存
//							GlobalApplication.appDB.deleteByWhere(
//									Product.class, null);
//							for (Product prods : mProducts) {
//								GlobalApplication.appDB.save(prods);
//							}
//						} else {
//							showCustomToast(desc);
//						}
//					}
//				} catch (Exception e) {
//					WuLog.e(TAG, "" + e.toString());
//				}
//
//			}
//		});
	}

	@Override
	protected void handleMsg(Message msg) {
		super.handleMsg(msg);
		switch (msg.what) {
		case 1111:
			break;

		default:
			break;
		}
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
	}

	@Override
	public void onCancel() {
		WuLog.i(TAG, "取消操作");
		clearAsyncTask();
		znmRL.onRefreshComplete();
	}

	@Override
	public void onRefresh() {
		WuLog.i(TAG, "刷新操作");
		getProductsFromNet();
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		WuLog.i(TAG, "条目点击操作");
	}

}
